package com.seu.githubcn.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.seu.githubcn.config.Result;
import com.seu.githubcn.entity.Manager;
import com.seu.githubcn.entity.Repo;
import com.seu.githubcn.entity.User;
import com.seu.githubcn.mapper.ManagerMapper;
import com.seu.githubcn.mapper.RepoMapper;
import com.seu.githubcn.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping
public class ManagerController {

    @Autowired
    ManagerMapper managerMapper;

    @Autowired
    RepoMapper repoMapper;

    @Autowired
    UserMapper userMapper;

    @PostMapping("/manager/new")
    public Result<?> newManager(@RequestBody Manager manager){
        Repo repo =
                repoMapper.selectOne(Wrappers.<Repo>lambdaQuery()
                        .eq(Repo::getRepoName, manager.getRepoName()));
        Manager isExist =
                managerMapper.selectOne(Wrappers.<Manager>lambdaQuery()
                        .eq(Manager::getManagerId,manager.getManagerId())
                        .eq(Manager::getRepoId,repo.getRepoId()));
        if(isExist!=null){
            return new Result<>(400,null,"该用户已为管理员！");
        }
        Manager newOne = new Manager();
        newOne.setManagerId(manager.getManagerId());
        newOne.setRepoId(repo.getRepoId());
        newOne.setManagerName(manager.getManagerName());
        newOne.setRepoName((repo.getRepoName()));
        managerMapper.insert(newOne);
        return new Result<>(200,null,"新增成功！");
    }

    @PostMapping("/manager/delete")
    public Result<?> deleteManager(@RequestBody Manager manager){
        Repo repo =
                repoMapper.selectById(manager.getRepoId());

        if(repo.getOwnerId().equals(manager.getManagerId())){
            return new Result<>(400,null,"仓库所有者不能被删除！");
        }
        managerMapper.delete(Wrappers.<Manager>lambdaQuery()
                .eq(Manager::getManagerId,manager.getManagerId())
                .eq(Manager::getRepoId,repo.getRepoId()));
        return new Result<>(200,null,"删除成功！");
    }

    @GetMapping("/{userName}/{repoName}/managers")
    public Result<?> managerList(@RequestParam("userName") String userName,
                                 @RequestParam("repoName") String repoName){
        User user =
                userMapper.selectOne(Wrappers.<User>lambdaQuery()
                        .eq(User::getUserName,userName));

        Repo repo =
                repoMapper.selectOne(Wrappers.<Repo>lambdaQuery()
                        .eq(Repo::getOwnerId,user.getUserId())
                        .eq(Repo::getRepoName, repoName));

        var managerList =
                managerMapper.selectList(Wrappers.<Manager>lambdaQuery()
                        .eq(Manager::getRepoId,repo.getRepoId()));

        for(var manager: managerList){
            User managerUser =
                    userMapper.selectById(manager.getManagerId());
            manager.setManagerName(managerUser.getUserName());
        }

        return new Result<>(200,managerList,"查询成功！");
    }

    @PostMapping("/manager/check")
    public Result<?> checkIsManager(@RequestBody Manager manager){
        Repo repo =
                repoMapper.selectOne(Wrappers.<Repo>lambdaQuery()
                        .eq(Repo::getRepoName, manager.getRepoName())
                        .eq(Repo::getOwnerName,manager.getOwnerName()));
        Manager isExist =
                managerMapper.selectOne(Wrappers.<Manager>lambdaQuery()
                        .eq(Manager::getManagerId,manager.getManagerId())
                        .eq(Manager::getRepoId,repo.getRepoId()));
        if(isExist!=null){
            return new Result<>(200,true,"该用户为管理员！");
        }
        return new Result<>(200,false,"该用户非管理员！");
    }



}
